package com.google.code.jtasks.model;

/**
 * Used to associate an {@link ITaskProperty} with a value for it.
 * 
 * @param <O>
 *            the type of the "owner" class: the class that the {@link ITaskProperty} is contained in
 */
public final class TaskPropertyValue<O>
{
	private final ITaskProperty<O> property;
	private final Object value;

	/**
	 * Constructs a new {@link TaskPropertyValue} instance.
	 * 
	 * @param property
	 *            the {@link ITaskProperty} that the value is associated with
	 * @param value
	 *            the value of the {@link ITaskProperty} to store
	 */
	public TaskPropertyValue(ITaskProperty<O> property, Object value)
	{
		// Sanity checks:
		if (property == null)
			throw new IllegalArgumentException("Property required.");
		if (value != null && !property.getValueType().isAssignableFrom(value.getClass()))
			throw new IllegalArgumentException(String.format("Value '%s' not compatible with property '%s'.",
					value, property.getName()));

		this.property = property;
		this.value = value;
	}

	/**
	 * Returns the {@link ITaskProperty} that this {@link TaskPropertyValue} is for.
	 * 
	 * @return the {@link ITaskProperty} that this {@link TaskPropertyValue} is for
	 */
	public ITaskProperty<O> getProperty()
	{
		return property;
	}

	/**
	 * Returns the value of the {@link ITaskProperty} that is stored in this {@link TaskPropertyValue}.
	 * 
	 * @return the value of the {@link ITaskProperty} that is stored in this {@link TaskPropertyValue}
	 */
	public Object getValue()
	{
		return value;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString()
	{
		StringBuilder debugText = new StringBuilder();
		debugText.append(this.getClass().getSimpleName());
		debugText.append("[");
		debugText.append("property=");
		debugText.append(property);
		debugText.append(",value=");
		debugText.append(value);
		debugText.append("]");
		return debugText.toString();
	}
}